﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Media;

namespace DemoGame_bird2._0
{
    /// <summary>
    /// 游戏类
    /// </summary>
    class Game
    {
        //游戏背景尺寸
        private const int GameWidth = 850;
        private const int GameHeight = 600;

        //初始化小鸟通过的柱子数量为num
        public int num = 0;

        //初始化上圆柱高度ColumnUpY
        private int ColumnUpY;

        
        //游戏对象——小鸟的初始化
        private LittleBird _LittleBird { get; set; }

        /// <summary>
        /// 所有障碍物圆柱类的集合
        /// </summary>
        private List<Barrier_Column> _Barrier_Columns { get; set; }

        //游戏对象自带的Timer
        private System.Timers.Timer Timer { get; set; }

        //定义委托
        public delegate void GameChanged();
        public delegate void GameOver();

        //定义事件
        public event GameChanged gameChanged;
        public event GameChanged gameOver;

        //定义下一组圆柱产生的时间
        private DateTime nextcolumntime { get; set; }

        //Isgamestart游戏开始
        public bool Isgamestart = false;


        /// <summary>
        /// 初始化游戏积分事件 
        /// </summary>
        public int Score { get; set; }



        /// <summary>
        /// 游戏类初始化
        /// </summary>
        public Game()
        {

            //初始化最初分数
            Score = 0;
            
            //初始化小鸟
            _LittleBird = new LittleBird(new Rectangle(150, 250, 50, 50));

            //初始化障碍物圆柱
            _Barrier_Columns = new List<Barrier_Column>();

            //为了让nextcolumntime进入if判断里
            nextcolumntime = System.DateTime.Now.AddDays(-1);

          
            //游戏对象的初始化时钟
            Timer = new System.Timers.Timer();
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();

           
        }



        /// <summary>
        /// 时钟处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //时钟关闭
            Timer.Stop();


            //按键“开始游戏”，游戏启动
            if (Isgamestart)
            {
                //小鸟可以上下移动
                _LittleBird.Move();

            }


            if (nextcolumntime < System.DateTime.Now && Isgamestart)
            {

                //随机产生上圆柱的高，变化范围是（100,300）
                Random random = new Random();
                int columnY = random.Next(100, 300);

                //定义圆柱起始运动位置
                Rectangle reccolumn = new Rectangle(800, 0, 80, 600);

                //每隔3秒产生下一组圆柱
                _Barrier_Columns.Add(new Barrier_Column(reccolumn, columnY));
                nextcolumntime = System.DateTime.Now.AddSeconds(3);
            }
            


            foreach (var _Barrier_Column in _Barrier_Columns)
            {

                //按键“开始游戏”，游戏启动
                if (Isgamestart)
                {                
                    //障碍物圆柱从左向右移动
                    _Barrier_Column.Move();

                }


            }


            //处理小鸟和圆柱的碰撞并记录得分——

            //定义分数=通过小鸟的圆柱数量，即小鸟左边位置的圆柱数量
            Score = _Barrier_Columns.Count(m => m.Rectangle.X < _LittleBird.Rectangle.X);
            
            //定义n为产生柱子数量
            for (int n = 0; n < _Barrier_Columns.Count; n++)
            {
                //情况一：当小鸟的rectangle与障碍物圆柱的rectangle相交 && 小鸟与上半圆柱相碰
                //情况二：当小鸟的rectangle与障碍物圆柱的rectangle相交 && 小鸟与下半圆柱相碰
                //两种情况下，游戏均失败，计时停止
                if (_LittleBird.Rectangle.IntersectsWith(_Barrier_Columns[n].Rectangle) &&
                    _LittleBird.Rectangle.Y < _Barrier_Columns[n].ColumnUpY |
                    _LittleBird.Rectangle.Y > _Barrier_Columns[n].ColumnUpY + 150)
                {
                    num = -1;

                    //触发“游戏失败”音效事件
                    SoundPlayer player = new SoundPlayer(Properties.Resources.lose);
                    player.Play();

                    //时钟停止
                    Timer.Stop();
                    break;
                }

                //小鸟完美穿过圆柱间隙，游戏继续，圆柱数量不断累积,积分不断+1
                else if (_LittleBird.Rectangle.X> _Barrier_Columns[n].Rectangle.X &&
                    _LittleBird.Rectangle.X < _Barrier_Columns[n].Rectangle.X+12 &&
                     _LittleBird.Rectangle.Y > _Barrier_Columns[n].ColumnUpY &&
                     _LittleBird.Rectangle.Y < _Barrier_Columns[n].ColumnUpY + 150)
                {
                    num++;
                   
                }


                //当小鸟通过圆柱数量i为10的时候，游戏胜利，happy ending，得到最终分数
                else if (num >= 10)
                {
                    //触发“游戏胜利”音效事件
                    SoundPlayer player = new SoundPlayer(Properties.Resources.win);
                    player.Play();

                    //时钟停止
                    Timer.Stop();
                    break;
                }


                //当小鸟飞至地面或者天空最顶部，游戏均失败，计时结束
                else if (_LittleBird.Rectangle.Y >= 550|
                         _LittleBird.Rectangle.Y <= 0)
                {
                    num = -1;

                    //触发“游戏失败”音效事件
                    SoundPlayer player = new SoundPlayer(Properties.Resources.lose);
                    player.Play();

                    //时钟停止
                    Timer.Stop();
                    break;

                }


            }


            //通知form游戏改变了
            gameChanged?.Invoke();

            //通知formmain判断游戏是否结束
            gameOver?.Invoke();

            //时钟开启
            Timer.Start();

        }



        /// <summary>
        /// 绘制游戏场景
        /// </summary>
        /// <param name="g">绘图句柄</param>
        /// <param name="size">游戏区域尺寸</param>
        public void Draw(Graphics g, Size size)
        {
            //定义空白的图
            Image img = new Bitmap(GameWidth, GameHeight);

            Graphics _g = Graphics.FromImage(img);

            //画背景:定义一个矩形，长宽是预定义
            Rectangle rec = new Rectangle(new Point(0, 0), new Size(GameWidth, GameHeight));

            //背景图片
            _g.DrawImage(Properties.Resources.bg, rec);

            //重新调用游戏对象——小鸟
            _LittleBird.Draw(_g);

            //重新调用游戏对象——障碍物圆柱
            for (int i = 0; i < _Barrier_Columns.Count; i++)
            {
                _Barrier_Columns[i].Draw(_g);
            }
            //todo

            g.DrawImage(img, new Rectangle(new Point(0, 0), size));


        }


        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(String key)
        {

            //触发“小鸟移动”事件         
            _LittleBird.KeyDown(key);

            //触发“小鸟飞翔跳跃”音效事件
            SoundPlayer player = new SoundPlayer(Properties.Resources.puleng);
            player.Play();

        }
    }
}
